package forecasting.converter.parser;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import forecasting.converter.FeatureType;
import forecasting.converter.RealValue;
import forecasting.util.FileUtils;

public class DataScrubParser extends AbstractDatasetParserTemplate {
	
	// Remove timestamps from dates				 					
    private Pattern timeStampPattern = Pattern.compile("(\\d{4}-\\d{2}-\\d{2})T\\d{2}:\\d{2}");

	public DataScrubParser(boolean saveCategories) {
		super(saveCategories);
		
	}		
    
	@Override
	protected void createOutputFile(final ArrayList<FeatureType> labels,
			final String inputFileName, final String outputFileName) {
		
		final Scanner scan = FileUtils.openFileScanner(inputFileName);				
		PrintWriter outfile = null;
		
		try {
			outfile = new PrintWriter(outputFileName);											
		} catch (FileNotFoundException e) {
			System.out.println(e);
			e.printStackTrace();
			return;
		}
									
		writeHeaderRow(labels, outfile, scan);
		int sampleNum = 0;
		
		while(scan.hasNextLine()) {
			final String line = scan.nextLine();
			
			++sampleNum;
			if (sampleNum % 10000 == 0) {
				System.out.println("Scrubbing file: Processing sample #" + sampleNum);
			}
			
			// checking for empty rows in the input dataset.
			if (line.length() == 0) {				
				continue;
			}
									
			final String[] attributeValues = line.substring(0, line.length() - 1).split("\t", -1);
			for (int i = 0; i < attributeValues.length; i++) {
				
				final FeatureType feature = labels.get(i);
				String value = attributeValues[i];
								
				// Transform all numbers
				if (feature instanceof RealValue && !value.equals("")) {										
					value = String.valueOf(((RealValue) feature).transform(
			    			RealValue.convertToDouble(value)));
			    }
			    
				// Remove quotes and double quotes
				value = value.replace("\"", "").replace("\'", "");
								
				// Turn the timestamp into a simple date. 
				final Matcher m = timeStampPattern.matcher(value);
	
				if (m.find()) {
					value = m.group(1);//print out the timestamp
				}
				
				
				outfile.write(value);
				outfile.write('\t');
				//if (i != attributeValues.length - 1) {
					
				//}
																
			}
			outfile.write('\n');
		}
		
		outfile.close();		
	}
	
	private void writeHeaderRow(final ArrayList<FeatureType> labels,
			final PrintWriter outfile, final Scanner scan) {		
		outfile.write(scan.nextLine() + '\n');		
	}
}